/**
 * 流程发起
 * @param  {[type]} processObject [{
 *     title:"测试流程001",
 *     businessId:"项目ID等",
 *     procDefKey:"qingjia"
 * }]
 * @return {[type]}               [description]
 */

/*
startProcess({
	"title":"测试流程",
	"businessId":"1234567890",
	"procDefKey":"qingjia"
})*/


function startProcess(processObject){
	configureCom.postAjax("/api/act/ctx/save",processObject).then(function(res){
		next({
			"procDefKey":processObject.procDefKey,
			"poId":res.data.poId
		},null);
	});
}


/**
 * [processOutputCtx description]
 * @type {Object}
 */
var processOutputCtx = {};

/**
 * [processInputCtx description]
 * @type {Object}
 */
//var processInputCtx = {}
//var buildProcessInputCtx = {};
var getParticipantsVariables = {};
/**
 * 获取下一步，打开下一步页面
 * @param  {[type]}   processCtxParameter [description]
 * @return {Function}                     [description]
 */
function next(processCtxParameter,opinion){
	configureCom.postAjax("/api/act/ctx/next",processCtxParameter).then(function(res){
		processOutputCtx = res.processOutputCtx;
		layer.open({
	        type: 2 //此处以iframe举例
	        ,title: '下一步：'
	        ,area: ['1000px', '800px']
	        ,shade: 0
	        ,maxmin: true
	        ,content: "/templates/act/next.html"
	        ,btn: ['提交']
	        ,yes: function(){
				//获取选节点和人的 variables
	        	var participantsVariables = getParticipantsVariables();
	        	participantsVariables.forEach(function(p){
	        		processOutputCtx.variables.forEach(function(v,index){
	        			if(p.name===v.name){
	        				processOutputCtx.variables.splice(index,1)
	        			}
	        		});
	        	});
	          	commit({
					"taskId"	:processOutputCtx.taskId,
					"procDefId"	:processOutputCtx.procDefId,
					"poId"		:processOutputCtx.poId,
					"variables"	:participantsVariables.concat(processOutputCtx.variables),
					"opinion"	:opinion,
				});
	        }
	        ,zIndex: layer.zIndex
	    });
	});
}

/*
使用next提交流程
next({
	"taskId":taskInfo.taskId,
	"poId":taskInfo.poId,
	"variables":[
		{"name":"pass","value":data.field.pass}
	]
},{
	"pass":data.field.pass,
	"passName":$('input:radio:checked').attr('title'),
	"comments":data.field.comments
});*/



/**
 * 启动流程
 * @param  {[type]} processInputCtx [description] :
 *  {
		"taskId"	:processOutputCtx.taskId,
		"procDefId"	:processOutputCtx.procDefId,
		"poId"		:processOutputCtx.poId,
		"variables"	:participantsVariables.concat(processOutputCtx.variables),
		"opinion"	:opinion,
	}

 * @return {[type]}                 [description]
 */
function commit(processInputCtx){
	configureCom.postAjax("/api/act/ctx/commit",processInputCtx).then(function(res){
		if(res.msg=='success'){
			layer.alert('提交成功!');
		}
	});
}